"""
清算相关模型
"""

from jlpay.model.base import OrgBaseReq, BaseResponse


# ==================== 提现查询 ====================
class WithdrawQueryRequest(OrgBaseReq):
    """提现查询请求"""
    
    API_PREFIX = "/fund/withdraw"
    API_URI = "/query"
    
    def __init__(self):
        """初始化提现查询请求"""
        super().__init__()
        self.out_settle_id = None
    
    def set_out_settle_id(self, out_settle_id):
        """
        设置外部结算ID
        
        Args:
            out_settle_id: 外部结算ID
            
        Returns:
            WithdrawQueryRequest实例
        """
        self.out_settle_id = out_settle_id
        return self


class WithdrawQueryResponse(BaseResponse):
    """提现查询响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化提现查询响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.settle_type = None
        self.amount = None
        self.result_state = None
        self.payment_time = None
        self.settle_id = None
        self.out_settle_id = None
        self.fee_amount = None
        self.postscript = None
        
        if response_dict:
            self.settle_type = response_dict.get('settle_type')
            self.amount = response_dict.get('amount')
            self.result_state = response_dict.get('result_state')
            self.payment_time = response_dict.get('payment_time')
            self.settle_id = response_dict.get('settle_id')
            self.out_settle_id = response_dict.get('out_settle_id')
            self.fee_amount = response_dict.get('fee_amount')
            self.postscript = response_dict.get('postscript')


# ==================== 提现申请 ====================
class WithdrawApplyRequest(OrgBaseReq):
    """提现申请请求"""
    
    API_PREFIX = "/fund/withdraw"
    API_URI = "/apply"
    
    def __init__(self):
        """初始化提现申请请求"""
        super().__init__()
        self.amount = None
        self.settle_type = None
        self.out_settle_id = None
        self.notify_url = None
        self.postscript = None
    
    def set_amount(self, amount):
        """
        设置金额
        
        Args:
            amount: 金额
            
        Returns:
            WithdrawApplyRequest实例
        """
        self.amount = amount
        return self
    
    def set_settle_type(self, settle_type):
        """
        设置结算类型
        
        Args:
            settle_type: 结算类型 (T0/T1)
            
        Returns:
            WithdrawApplyRequest实例
        """
        self.settle_type = settle_type
        return self
    
    def set_out_settle_id(self, out_settle_id):
        """
        设置外部结算ID
        
        Args:
            out_settle_id: 外部结算ID
            
        Returns:
            WithdrawApplyRequest实例
        """
        self.out_settle_id = out_settle_id
        return self
    
    def set_notify_url(self, notify_url):
        """
        设置通知地址
        
        Args:
            notify_url: 通知地址
            
        Returns:
            WithdrawApplyRequest实例
        """
        self.notify_url = notify_url
        return self
    
    def set_postscript(self, postscript):
        """
        设置附言
        
        Args:
            postscript: 附言
            
        Returns:
            WithdrawApplyRequest实例
        """
        self.postscript = postscript
        return self


class WithdrawApplyResponse(BaseResponse):
    """提现申请响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化提现申请响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.amount = None
        self.settle_type = None
        self.result_state = None
        self.result_msg = None
        self.settle_id = None
        self.out_settle_id = None
        self.fee_amount = None
        
        if response_dict:
            self.amount = response_dict.get('amount')
            self.settle_type = response_dict.get('settle_type')
            self.result_state = response_dict.get('result_state')
            self.result_msg = response_dict.get('result_msg')
            self.settle_id = response_dict.get('settle_id')
            self.out_settle_id = response_dict.get('out_settle_id')
            self.fee_amount = response_dict.get('fee_amount')


# ==================== 文件上传 ====================
class FileUploadRequest:
    """文件上传请求"""
    
    API_PREFIX = "/base/file/upload"
    API_URI = "/video"
    
    def __init__(self):
        """初始化文件上传请求"""
        self.file_name = None
        self.alg = None
        self.file = None
    
    def set_file_name(self, file_name):
        """
        设置文件名
        
        Args:
            file_name: 文件名
            
        Returns:
            FileUploadRequest实例
        """
        self.file_name = file_name
        return self
    
    def set_alg(self, alg):
        """
        设置算法类型
        
        Args:
            alg: 算法类型 (SM3/SHA256)
            
        Returns:
            FileUploadRequest实例
        """
        self.alg = alg
        return self
    
    def set_file(self, file):
        """
        设置文件内容
        
        Args:
            file: 文件内容 (bytes)
            
        Returns:
            FileUploadRequest实例
        """
        self.file = file
        return self
    
    def to_dict(self):
        """
        将请求对象转换为字典，过滤掉值为None的字段
        
        Returns:
            字典对象
        """
        import base64
        
        result = {}
        for k, v in self.__dict__.items():
            if not k.startswith('_') and v is not None:
                # 如果是 bytes 类型，转换为 Base64 字符串
                if isinstance(v, bytes):
                    result[k] = base64.b64encode(v).decode('utf-8')
                else:
                    result[k] = v
        return result
    
    def get_meta_dict(self):
        """
        获取 multipart 请求的元数据字典
        
        Returns:
            元数据字典
        """
        return {
            'filename': self.file_name,
            'alg': self.alg
        }


class FileUploadResponse(BaseResponse):
    """文件上传响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化文件上传响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.media_id = None
        
        if response_dict:
            self.media_id = response_dict.get('media_id')


# ==================== 结算资金查询 ====================
class AccBalanceRequest(OrgBaseReq):
    """结算资金查询请求"""
    
    API_PREFIX = "/fund/settlement"
    API_URI = "/account/balance"
    
    def __init__(self):
        """初始化结算资金查询请求"""
        super().__init__()
        self.acct_type = None
    
    def set_acct_type(self, acct_type):
        """
        设置账户类型
        
        Args:
            acct_type: 账户类型
            
        Returns:
            AccBalanceRequest实例
        """
        self.acct_type = acct_type
        return self


class AccBalanceResponse(BaseResponse):
    """结算资金查询响应"""
    
    def __init__(self, response_dict=None):
        """
        初始化结算资金查询响应
        
        Args:
            response_dict: 响应字典
        """
        super().__init__(response_dict)
        self.balance = None
        self.available_balance = None
        self.frozen_amount = None
        self.status = None
        
        if response_dict:
            self.balance = response_dict.get('balance')
            self.available_balance = response_dict.get('available_balance')
            self.frozen_amount = response_dict.get('frozen_amount')
            self.status = response_dict.get('status')


# ==================== 通知DTO ====================
class NotifyDto:
    """通知数据传输对象"""
    
    def __init__(self, response_dict=None):
        """
        初始化通知DTO
        
        Args:
            response_dict: 响应字典
        """
        self.merch_no = None
        self.settle_type = None
        self.amount = None
        self.fee_amount = None
        self.out_settle_id = None
        self.settle_id = None
        self.postscript = None
        self.payment_time = None
        self.org_no = None
        self.result_state = None
        
        if response_dict:
            self.merch_no = response_dict.get('merch_no')
            self.settle_type = response_dict.get('settle_type')
            self.amount = response_dict.get('amount')
            self.fee_amount = response_dict.get('fee_amount')
            self.out_settle_id = response_dict.get('out_settle_id')
            self.settle_id = response_dict.get('settle_id')
            self.postscript = response_dict.get('postscript')
            self.payment_time = response_dict.get('payment_time')
            self.org_no = response_dict.get('org_no')
            self.result_state = response_dict.get('result_state') 